<?php
/**
 * Aero - Xistins Technologies PHP Framework
 * 
 * LICENSE
 * This source file is subject to the New BSD license that is bundled 
 * with this package in the LICENSE.txt. It is also available through 
 * the world-wide-web at this URL: http://dev.xistins.com/license/production
 * If you did not receive a copy of this license and are unable to obtain
 * it through the world-wide-web, please send an email to 
 * license@xistins.com so we can send you a copy immediately.
 * 
 * @category   Aero
 * @package    Aero_Db
 * @subpackage Adapter
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Sqlite.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */

/** @see Aero_Db_Adapter */
require_once 'Aero/Db/Adapter.php';

/**
 * SQLite adapter
 * 
 * @category   Aero
 * @package    Aero_Db
 * @subpackage Adapter
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Sqlite.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */
class Aero_Db_Adapter_Sqlite extends Aero_Db_Adapter
{
    /**
     * Process configuration file
     * 1. Make sure it is of the right type (Convert to stdClass)
     * 2. Check for stdClass, hostname, username, and password.
     * 
     * @param  array|stdClass $config
     * @return stdClass
     */
    protected function _processConfig($config)
    {
        // Check config type
        if(!(array) $config) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('Configuration supplied to Aero_Db must either be an array
                                         or a standard object.');
        }
        $config = (object)$config;

        // Check for hostname, username, password, and dbname
        if(!isset($config->dbname)) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('Configuration must contain your SQLite database file name.');
        }

        return $config;
    }

    /**
     * Connect to the database
     * 
     * @return void
     */
    protected function _connect()
    {
        $this->_connection = @sqlite_open($this->_config->dbname);
        
        if(!$this->_connection) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('Unable to load or create SQLite database');
        }
    }

    /**
     * Close database connection
     * 
     * @return void
     */
    public function closeConnection()
    {
        sqlite_close($this->_connection);
        $this->_connection = NULL;
    }

    /**
     * Describe Table
     * 
     * @param  string $tableName
     * @return array
     */
    public function describeTable($tableName)
    {
        /**
         * @TODO This is not a simple task with sqlite. We'll revisit this in a future update.
         */
         return;
    }

    /**
     * Escape a variable for query usage
     * 
     * @param  string|int|float $var
     * @return string
     */
    public function escape($var)
    {
        if(!is_string($var) && !is_int($var) && !is_float($var)) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('Variable needing escaped must be a string, integer, or float.');
        }

        return sqlite_escape_string($var);
    }

    /**
     * Execute a resultless query
     * 
     * @param  string $sql
     * @return bool
     */
    public function exec($sql)
    {
        $this->getConnection();
        return sqlite_exec($sql, $this->_connection);
    }

    /**
     * List all tables in the database
     * 
     * @param  bool $descTables Describe tables
     * @return mixed
     */
    public function listTables($descTables = false)
    {
        $tableResult = $this->query('SELECT name FROM sqlite_master WHERE type=\'table\' ORDER BY name;');
        if(!$tableResult) {
            return false;
        }

        foreach($tableResult as $table) {
            $tableName = $table['name'];
            unset($table['name']);
            $tables[] = $tableName;
        }
        return $tables;
    }

    /**
     * Run query on the database
     * 
     * @return array|bool
     */
    public function query($sql)
    {
        $this->getConnection();
        $results = @sqlite_query($sql, $this->_connection);

        if(!$results) {
            return false;
        }

        while ($row = sqlite_fetch_array($results)) {
            $return[] = $row;
        }
        unset($results);
        return $return;
    }
}